<link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>file/shell/smooth/resources/css/jquery.multiselect.css">
<script src="<?php echo base_url(); ?>file/shell/smooth/resources/scripts/jquery.multiselect.min.js"></script>
<script src="<?php echo base_url(); ?>file/js/accounting.min.js"></script>

<div class="page-title">
  <h2>Penjualan E-Load</h2>
</div>

<?php 
  $message = $this->session->flashdata('message');
  if ($message) : $div_class = $this->session->flashdata('div_class');
?>
  <div class="<?php $div_class; ?>">
    <?php echo $message; ?>
  </div>
<?php endif; ?>

<div class="page page-trans">
  
<?php 
  function backdate($param) {
    if ($param > 1) {
      echo '<select id="date" name="sell_in_date">';
      for ($i=0; $i < $param; $i++) { 
        echo '<option value="'.date("Y-m-j", strtotime("-".$i." day")).'">'.date("m/d/Y", strtotime("-".$i." day")).'</option>';
      }
      echo '</select>';
    }
    else 
    {
      echo '<input type="text" name="sell_in_date" id="date" placeholder="yyyy-mm-dd" value="'.date("Y-m-d").'">';
    }
  }

?>

<form action="<?php echo base_url('index.php/sell/save_eload'); ?>" method="post" >
  <table class="general">
    <thead>
      <tr>
        <th>Territory</th>
        <th><strong><?php echo $region.', '.$cluster; ?></strong></th>
      </tr>
    </thead>
    <tr>
      <td>Tanggal Sell In</td>
      <td>
        <!-- <input type="text" name="sell_in_date" id="date" placeholder="yyyy-mm-dd" value="<?php echo date("Y-m-d")?>"> -->
        <?php backdate(2); ?>
        <img src="<?php echo base_url('file/images/information.png'); ?>" title="Jika muncul dropdown, diperbolehkan untuk backdate">
      </td>
    </tr>
    <tr>
      <td>Nama Sales</td>
      <td>
        <select name="sales_id" id="sales-id">
          <option value=""></option>
            <?php foreach ($sales as $key) : ?>
                <option value="<?php echo $key->user_id.';'.$key->user_group_id; ?>"><?php echo $key->user_id.' | '.$key->user_group_caption.' | '.$key->user_name; ?></option>
            <?php endforeach; ?>
        </select>
      </td>
    </tr>
    <tr>
      <td></td>
      <td><button name="" id="load" >Tampilkan Partner</button></td>
    </tr>
  </table>

  <div>
    <table class="tabel_transaksi" width="100%">
      <thead>
        <tr>
          <td>#</td>
          <td>ID Partner</td>
          <td>Nama Partner</td>
          <td>Nomor Eload</td>
          <td>Nominal Eload</td>
        </tr>
      </thead>
      <tfoot>
        <tr>
          <td colspan="4" style="text-align: right">Nominal</td>
          <td class="total"></td>
        </tr>

        <tr>
          <td colspan="5" class="align-center">
            <button class="button" id="button-simpan" role="button" >Simpan</button>
            <button class="button" id="button-reset" role="reset" >Batal</button>
          </td>
        </tr>
      </tfoot>
      <tbody class="channel-name">
        <!-- di sini di append data agent/outlet -->
      </tbody>
    </table>
    
  </div>
</form>
</div>


<script type="text/javascript">
$('button#load').click(function(event){
  event.preventDefault();
  $('tbody.channel-name').empty();
  if( $('#sales-id').val() ) {
    var _v = $('#sales-id').val().split(";");
    $.post(
        "<?php echo base_url('index.php/jc/get_eload_jc'); ?>",
        {user_id: _v[0], group_id: _v[1], trans_date: $('#date').val()},
        function(data){
          $('tbody.channel_name').empty();
          var _d = jQuery.parseJSON(data);
          var iterasi = 1;
          $.each(_d, function(k, v) {
            var _r = "<tr class='channel_id_"+iterasi+"'><td>"+iterasi+"</td>"+
                     "<td class='channel_id'>"+v['channel_id']+"<input type='hidden' name='channel_id[]' id='channel_id_"+iterasi+"' value='"+v['channel_id']+"' /></td>"+
                     "<td>"+v['channel_name']+"</td>"+
                     "<td>"+v['eload_number']+"<input type='hidden' name='eload_num[]' id='eload_num_"+iterasi+"' value='"+v['eload_number']+"' /></td>"+
                     "<td>Rp. <input class='eload_nominal' id='eload_nominal_"+iterasi+"' name='eload_nominal[]' onkeyup='jumlah()' value='0' /></td></tr>";
            $('tbody.channel-name').append(_r);
            iterasi++;
          });
        }
      );
  }
  else
  {
    alert('Nama sales tidak boleh kosong');
    // event.preventDefault();
  }

});

$('button#button-simpan').click(function(event){
  $('input.eload_nominal')
});

$('button#button-reset').click(function(event){
  $('input.eload_nominal').val('0');
});


function jumlah() {
  var _j = parseInt($('.eload_nominal').length);
  var _sum = 0;
  for (var i=1; i <= _j; i++) { 
    var _s = parseInt($('#eload_nominal_'+i).val());
    var _sum = _sum + _s;
  };
  $('.total').html(accounting.formatMoney(_sum, "Rp. ", 2, ".", ","));
  // fx('.total').to("IDR")
}
</script>